<script setup>
import { format } from '@/util/format.js'
import { Delete, Edit } from '@element-plus/icons-vue'
import channelSelect from './components/channelSelect.vue'
import { ref, watch } from 'vue'
import { artGetListService } from '@/api/article.js'
import articleEdit from './components/articleEdit.vue'
import { artDelListService } from '@/api/article'

const params = ref({
  pagenum: 1,
  pagesize: 4,
  cate_id: '',
  state: ''
})
const articleList = ref([])
const total = ref()
const loading = ref(false)
const artEdit = ref()

// 获取所有文章
const getList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
// 编辑文章
getList()
const onEditList = (row) => {
  // drawerShow.value = true
  artEdit.value.open(row)
}
// 删除文章
const onDelList = async (row) => {
  console.log(row)
  await ElMessageBox.confirm('确定要删除吗?', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })
  const res = await artDelListService(row.id)
  console.log(res)
  await getList()
}
// 发布文章
const onAddArticle = () => {
  artEdit.value.open({})
}
// 分页器
const handleSizeChange = () => {
  params.value.pagenum = 1
  getList()
}
const handleCurrentChange = () => {
  getList()
}

watch(
  () => params.value.pagesize,
  () => {
    handleSizeChange()
  }
)
watch(
  () => params.value.pagenum,
  () => {
    handleCurrentChange()
  }
)
// 搜索重置
const searchButton = () => {
  params.value.pagenum = 1
  getList()
}
const resetButton = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getList()
}

// 发布文章
const onSuccess = async (str) => {
  if (str === 'add') {
    params.value.pagenum = Math.ceil((total.value + 1) / params.value.pagesize)
    await getList()
  } else {
    await getList()
  }
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle">发布文章</el-button>
    </template>
    <el-form inline>
      <el-form-item label="文章分类">
        <channelSelect v-model="params.cate_id"></channelSelect>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select style="width: 220px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchButton">搜索</el-button>
        <el-button @click="resetButton">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题">
        <template #default="{ row }">
          <el-link type="primary" underline="never">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="cate_name" label="分类"></el-table-column>
      <el-table-column prop="pub_date" label="发布时间">
        <template #default="{ row }">
          {{ format(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button circle plain :icon="Edit" type="primary" @click="onEditList(row)"></el-button>
          <el-button circle plain :icon="Delete" type="danger" @click="onDelList(row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 4, 6, 10]"
      background
      layout="jumper,total, sizes, prev, pager, next"
      :total="total"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </page-container>
  <articleEdit ref="artEdit" @success="onSuccess"></articleEdit>
</template>
<style scoped lang="scsss"></style>
